package com.sxfq.loansupermarket.backsystem.modular.report.service.impl;

import com.sxfq.loansupermarket.backsystem.common.persistence.model.Dept;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.report.entity.MarketReportTwoInfo;
import com.sxfq.loansupermarket.backsystem.modular.report.entity.SearchMarketReportInfo;
import com.sxfq.loansupermarket.backsystem.modular.report.entity.SearchMarketReportTwoInfo;
import com.sxfq.loansupermarket.backsystem.modular.report.service.IGetTotleMarketTwoService;
import com.sxfq.loansupermarket.backsystem.modular.report.service.IMarketReportTwoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class MarketReportTwoServiceImpl implements IMarketReportTwoService {
    @Autowired
    private IGetTotleMarketTwoService service;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<MarketReportTwoInfo> getTreatmentUnitListTwo(SearchMarketReportTwoInfo searchInfo) {
        String sqldept = "select id,abbreviation,simplename,abbreviation from dept ";
        if (StringUtil.isNotEmpty(searchInfo.getArrivalStore())) {
            sqldept += " where simplename='" + searchInfo.getArrivalStore() + "'";
        } else {
            sqldept += " where simplename='" + ShiroKit.getUser().getOutpatientName() + "'";
        }
        Dept dept = sqlMapper.selectOne(sqldept, Dept.class);
        if ("人员".equals(searchInfo.getDimensionSelectionTwo())) {
            searchInfo.setDepartment(searchInfo.getDimensionSelection());
            String sql1 = " select t.id,t.archiving from reservations_infosc t left join "+dept.getAbbreviation()+"customer_info t1 on t.id=t1.reservations_id where " +
                    "((t1.arrival_time>='" + searchInfo.getStartTime() + "' and t1.arrival_time<='" + searchInfo.getEndTime() + "') or " +
                    "(t.filing_time>='" + searchInfo.getStartTime() + "' and t.filing_time<='" + searchInfo.getEndTime() + "') or " +
                    "(t.appointment_time>='" + searchInfo.getStartTime() + "' and t.appointment_time<='" + searchInfo.getEndTime() + "'))  " +
                    " and (t.arrival_store ='"+dept.getSimplename()+"' or t.outpatient_name='"+dept.getSimplename()+"') group by t.archiving";
            List<SearchMarketReportInfo> list1 = sqlMapper.selectList(sql1, SearchMarketReportInfo.class);
            String sql = "";
            String condition = "";
            String condition1 = "";
            String condition2 = "";
            String condition3 = "";
            for (int i = 0; i < list1.size(); i++) {
                String classification = list1.get(i).getArchiving();
                String channel = "'" + list1.get(i).getArchiving() + "' as archiving";
                sql += service.getsqlMarketPersonnel(searchInfo, channel, classification, i, list1, dept.getSimplename(), condition, condition1, condition2, condition3);
            }
            if(list1.size()>0){
                sql += " union all ";
            }
            String channel = "'合计' as archiving ";
            sql += service.getsqlHospitalWide(searchInfo, channel, dept.getSimplename(), condition, condition1, condition2, condition3);
            List<MarketReportTwoInfo> list = sqlMapper.selectList(sql, MarketReportTwoInfo.class);
            return list;
        } else if ("渠道".equals(searchInfo.getDimensionSelectionTwo())) {
            String sql1 = " select t.id,t.one_channel as oneChannel,t.two_channel as twoChannel,t.thr_channel as thrChannel from reservations_infosc t where 1=1";
            String conditions = "";
            String conditions1 = "";
            int a = 0;
            int b = 0;
            int c = 0;
            if (!"请选择".equals(searchInfo.getOneChannel()) && StringUtil.isNotEmpty(searchInfo.getOneChannel())) {
                a++;
                sql1 += " and t.one_channel='" + searchInfo.getOneChannel() + "'";
            }
            if (!"请选择".equals(searchInfo.getTwoChannel()) && StringUtil.isNotEmpty(searchInfo.getTwoChannel())) {
                b++;
                sql1 += " and t.two_channel='" + searchInfo.getTwoChannel() + "'";
            }
            if (!"请选择".equals(searchInfo.getThrChannel()) && StringUtil.isNotEmpty(searchInfo.getThrChannel())) {
                c++;
                sql1 += " and t.thr_channel='" + searchInfo.getThrChannel() + "'";
            }
            if (a == 0) {
                sql1 += " group by t.one_channel order by t.one_channel ";
            } else {
                if (b == 0) {
                    sql1 += " group by t.two_channel order by t.two_channel ";
                    conditions += " and t.one_channel='" + searchInfo.getOneChannel() + "' ";
                    conditions1 += " and t1.one_channel='" + searchInfo.getOneChannel() + "' ";
                } else {
                    if (c == 0) {
                        sql1 += " group by t.thr_channel order by t.one_channel ,t.two_channel ";
                        conditions += " and t.one_channel='" + searchInfo.getOneChannel() + "' and t.two_channel='" + searchInfo.getTwoChannel() + "' ";
                        conditions1 += " and t1.one_channel='" + searchInfo.getOneChannel() + "' and t1.two_channel='" + searchInfo.getTwoChannel() + "' ";
                    } else {
                        sql1 += " group by t.thr_channel order by t.one_channel ,t.two_channel,t.thr_channel ";
                        conditions += " and t.one_channel='" + searchInfo.getOneChannel() + "' and t.two_channel='" + searchInfo.getTwoChannel() + "' " +
                                " and t.thr_channel='" + searchInfo.getThrChannel() + "' ";
                        conditions1 += " and t1.one_channel='" + searchInfo.getOneChannel() + "' and t1.two_channel='" + searchInfo.getTwoChannel() + "' " +
                                " and t1.thr_channel='" + searchInfo.getThrChannel() + "' ";
                    }
                }
            }
            List<SearchMarketReportTwoInfo> list1 = sqlMapper.selectList(sql1, SearchMarketReportTwoInfo.class);
            String sql = "";
            String condition1 = "";
            String condition2 = "";
            String condition3 = "";
            String condition4 = "";
            for (int i = 0; i < list1.size(); i++) {
                String channel = "'" + list1.get(i).getOneChannel() + "' as oneChannel,'" + list1.get(i).getTwoChannel() + "' as twoChannel,'"
                        + list1.get(i).getThrChannel() + "' as thrChannel";
                String condition = "";
                String condition5 = "";
                if (a == 0) {
                    condition += " and t.one_channel='" + list1.get(i).getOneChannel() + "' ";
                    condition5 += " and t1.one_channel='" + list1.get(i).getOneChannel() + "' ";
                } else {
                    if (b == 0) {
                        condition += " and t.one_channel='" + list1.get(i).getOneChannel() + "' and t.two_channel='" + list1.get(i).getTwoChannel() + "' ";
                        condition5 += " and t1.one_channel='" + list1.get(i).getOneChannel() + "' and t1.two_channel='" + list1.get(i).getTwoChannel() + "' ";
                    } else {
                        if (c == 0) {
                            condition += " and t.one_channel='" + list1.get(i).getOneChannel() + "' and t.two_channel='" + list1.get(i).getTwoChannel() + "' " +
                                    " and t.thr_channel='" + list1.get(i).getThrChannel() + "'";
                            condition5 += " and t1.one_channel='" + list1.get(i).getOneChannel() + "' and t1.two_channel='" + list1.get(i).getTwoChannel() + "' " +
                                    " and t1.thr_channel='" + list1.get(i).getThrChannel() + "'";
                        } else {
                            condition += " and t.one_channel='" + list1.get(i).getOneChannel() + "' and t.two_channel='" + list1.get(i).getTwoChannel() + "' " +
                                    " and t.thr_channel='" + list1.get(i).getThrChannel() + "'";
                            condition5 += " and t1.one_channel='" + list1.get(i).getOneChannel() + "' and t1.two_channel='" + list1.get(i).getTwoChannel() + "' " +
                                    " and t1.thr_channel='" + list1.get(i).getThrChannel() + "'";
                        }
                    }
                }
                sql += service.getsqlDepartment(searchInfo, channel, condition, i, list1, dept.getAbbreviation(), dept.getSimplename(), condition1, condition2, condition3, condition4, condition5);
            }
            if(list1.size()>0){
                sql += " union all ";
            }
            String channels = "'合计' as oneChannel,'' as twoChannel,'' as thrChannel";
            sql += service.getTotleDepartmentSql(searchInfo, channels, conditions, dept.getAbbreviation(), condition1, condition2, condition3, condition4, conditions1);
            List<MarketReportTwoInfo> list = sqlMapper.selectList(sql, MarketReportTwoInfo.class);
            return list;
        } else {
            String sql1 = "select a.id,a.department from "+redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"pay_service_grade_info",0)+" a group by department order by a.id";
            List<SearchMarketReportTwoInfo> list1 = sqlMapper.selectList(sql1, SearchMarketReportTwoInfo.class);
            String sql = "";
            for (int i = 0; i < list1.size(); i++) {
                String classification = list1.get(i).getDepartment();
                String condition = "";
                condition = " b.treatment_unit='" + classification + "' ";
                sql += service.getsqlDepartmentTwo(searchInfo, classification, condition, i, list1);
            }
            if(list1.size()>0){
                sql += " union all ";
            }
            sql += service.getTotleSqlDepartmentTwo(searchInfo);
            List<MarketReportTwoInfo> list = sqlMapper.selectList(sql, MarketReportTwoInfo.class);
            return list;
        }
    }

    @Override
    public List<MarketReportTwoInfo> getTreatmentUnitListsTwo(SearchMarketReportTwoInfo searchInfo) {
        String sqldept = "select id,abbreviation,simplename,abbreviation from dept ";
        if (StringUtil.isNotEmpty(searchInfo.getArrivalStore())) {
            sqldept += " where simplename='" + searchInfo.getArrivalStore() + "'";
        } else {
            sqldept += " where simplename='" + ShiroKit.getUser().getOutpatientName() + "'";
        }
        Dept dept = sqlMapper.selectOne(sqldept, Dept.class);
        if ("人员".equals(searchInfo.getDimensionSelectionTwo())) {
            searchInfo.setDepartment(searchInfo.getDimensionSelection());
            String sql1 = " select t.id,t.archiving from reservations_infosc t left join "+dept.getAbbreviation()+"customer_info t1 on t.id=t1.reservations_id where " +
                    "((t1.arrival_time>='" + searchInfo.getStartTime() + "' and t1.arrival_time<='" + searchInfo.getEndTime() + "') or " +
                    "(t.filing_time>='" + searchInfo.getStartTime() + "' and t.filing_time<='" + searchInfo.getEndTime() + "') or " +
                    "(t.appointment_time>='" + searchInfo.getStartTime() + "' and t.appointment_time<='" + searchInfo.getEndTime() + "'))  " +
                    " and (t.arrival_store ='"+dept.getSimplename()+"' or t.outpatient_name='"+dept.getSimplename()+"') group by t.archiving";
            List<SearchMarketReportInfo> list1 = sqlMapper.selectList(sql1, SearchMarketReportInfo.class);
            String sql = "";
            String condition = " and t.appointment_pay_service='" + searchInfo.getDepartment() + "' ";
            String condition1 = " and t1.treatment_unit='" + searchInfo.getDepartment() + "' ";
            String condition2 = " and t2.treatment_unit='" + searchInfo.getDepartment() + "' ";
            String condition3 = " and t.treatment_unit='" + searchInfo.getDepartment() + "' ";
            for (int i = 0; i < list1.size(); i++) {
                String classification = list1.get(i).getArchiving();
                String channel = "'" + list1.get(i).getArchiving() + "' as archiving";
                sql += service.getsqlMarketPersonnel(searchInfo, channel, classification, i, list1, dept.getSimplename(), condition, condition1, condition2, condition3);
            }
            if(list1.size()>0){
                sql += " union all ";
            }
            String channel = "'合计' as archiving ";
            sql += service.getsqlHospitalWide(searchInfo, channel, dept.getSimplename(), condition, condition1, condition2, condition3);
            List<MarketReportTwoInfo> list = sqlMapper.selectList(sql, MarketReportTwoInfo.class);
            return list;
        } else if ("渠道".equals(searchInfo.getDimensionSelectionTwo())) {
            String sql1 = " select t.id,t.one_channel as oneChannel,t.two_channel as twoChannel,t.thr_channel as thrChannel from reservations_infosc t where 1=1";
            String conditions = "";
            String conditions1 = "";
            int a = 0;
            int b = 0;
            int c = 0;
            if (!"请选择".equals(searchInfo.getOneChannel()) && StringUtil.isNotEmpty(searchInfo.getOneChannel())) {
                a++;
                sql1 += " and t.one_channel='" + searchInfo.getOneChannel() + "'";
            }
            if (!"请选择".equals(searchInfo.getTwoChannel()) && StringUtil.isNotEmpty(searchInfo.getTwoChannel())) {
                b++;
                sql1 += " and t.two_channel='" + searchInfo.getTwoChannel() + "'";
            }
            if (!"请选择".equals(searchInfo.getThrChannel()) && StringUtil.isNotEmpty(searchInfo.getThrChannel())) {
                c++;
                sql1 += " and t.thr_channel='" + searchInfo.getThrChannel() + "'";
            }
            if (a == 0) {
                sql1 += " group by t.one_channel order by t.one_channel ";
            } else {
                if (b == 0) {
                    sql1 += " group by t.two_channel order by t.two_channel ";
                    conditions += " and t.one_channel='" + searchInfo.getOneChannel() + "' ";
                    conditions1 += " and t1.one_channel='" + searchInfo.getOneChannel() + "' ";
                } else {
                    if (c == 0) {
                        sql1 += " group by t.thr_channel order by t.one_channel ,t.two_channel ";
                        conditions += " and t.one_channel='" + searchInfo.getOneChannel() + "' and t.two_channel='" + searchInfo.getTwoChannel() + "'  ";
                        conditions1 += " and t1.one_channel='" + searchInfo.getOneChannel() + "' and t1.two_channel='" + searchInfo.getTwoChannel() + "'  ";
                    } else {
                        sql1 += " group by t.thr_channel order by t.one_channel ,t.two_channel,t.thr_channel ";
                        conditions += " and t.one_channel='" + searchInfo.getOneChannel() + "' and t.two_channel='" + searchInfo.getTwoChannel() + "' " +
                                " and t.thr_channel='" + searchInfo.getThrChannel() + "' ";
                        conditions1 += " and t1.one_channel='" + searchInfo.getOneChannel() + "' and t1.two_channel='" + searchInfo.getTwoChannel() + "' " +
                                " and t1.thr_channel='" + searchInfo.getThrChannel() + "' ";
                    }
                }
            }
            List<SearchMarketReportTwoInfo> list1 = sqlMapper.selectList(sql1, SearchMarketReportTwoInfo.class);
            String condition1 = " and t.appointment_pay_service='" + searchInfo.getDimensionSelection() + "' ";
            String condition2 = " and t.treatment_unit='" + searchInfo.getDimensionSelection() + "' ";
            String condition3 = " and t2.treatment_unit='" + searchInfo.getDimensionSelection() + "' ";
            String condition4 = "";
            String sql = "";
            for (int i = 0; i < list1.size(); i++) {
                String channel = "'" + list1.get(i).getOneChannel() + "' as oneChannel,'" + list1.get(i).getTwoChannel() + "' as twoChannel,'"
                        + list1.get(i).getThrChannel() + "' as thrChannel";
                String condition = "";
                String condition5 = "";
                if (a == 0) {
                    condition += " and t.one_channel='" + list1.get(i).getOneChannel() + "' ";
                    condition5 += " and t1.one_channel='" + list1.get(i).getOneChannel() + "' ";
                } else {
                    if (b == 0) {
                        condition += " and t.one_channel='" + list1.get(i).getOneChannel() + "' and t.two_channel='" + list1.get(i).getTwoChannel() + "' ";
                        condition5 += " and t1.one_channel='" + list1.get(i).getOneChannel() + "' and t1.two_channel='" + list1.get(i).getTwoChannel() + "' ";
                    } else {
                        if (c == 0) {
                            condition += " and t.one_channel='" + list1.get(i).getOneChannel() + "' and t.two_channel='" + list1.get(i).getTwoChannel() + "' " +
                                    " and t.thr_channel='" + list1.get(i).getThrChannel() + "'";
                            condition5 += " and t1.one_channel='" + list1.get(i).getOneChannel() + "' and t1.two_channel='" + list1.get(i).getTwoChannel() + "' " +
                                    " and t1.thr_channel='" + list1.get(i).getThrChannel() + "'";
                        } else {
                            condition += " and t.one_channel='" + list1.get(i).getOneChannel() + "' and t.two_channel='" + list1.get(i).getTwoChannel() + "' " +
                                    " and t.thr_channel='" + list1.get(i).getThrChannel() + "'";
                            condition5 += " and t1.one_channel='" + list1.get(i).getOneChannel() + "' and t1.two_channel='" + list1.get(i).getTwoChannel() + "' " +
                                    " and t1.thr_channel='" + list1.get(i).getThrChannel() + "'";
                        }
                    }
                }
                sql += service.getsqlDepartment(searchInfo, channel, condition, i, list1, dept.getAbbreviation(), dept.getSimplename(), condition1, condition2, condition3, condition4, condition5);
            }
            if(list1.size()>0){
                sql += " union all ";
            }
            String channels = "'合计' as oneChannel,'' as twoChannel,'' as thrChannel";
            sql += service.getTotleDepartmentSql(searchInfo, channels, conditions, dept.getAbbreviation(), condition1, condition2, condition3, condition4, conditions1);
            List<MarketReportTwoInfo> list = sqlMapper.selectList(sql, MarketReportTwoInfo.class);
            return list;
        } else {
            String condition = "b.treatment_unit='" + searchInfo.getDimensionSelection() + "'";
            String sql = service.getsqlDepartmentsTwo(searchInfo, searchInfo.getDimensionSelection(), condition);
            List<MarketReportTwoInfo> list = sqlMapper.selectList(sql, MarketReportTwoInfo.class);
            return list;
        }
    }
}